package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.RegionLevel;
import models.RegionLevelRelation;
import models.RegionLevel;
import models.Device;
import models.LevelModelPrice;
import models.Operator;
import models.Oplog;
import services.AuthorityService;
import services.RegionLevelService;
import services.DealerService;
import services.DeviceService;
import services.LevelModelPriceService;
import services.RegionLevelRelationService;
import services.RegionLevelService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import utils.Validate;
import vos.RegionLevelSeachVo;
import vos.ReturnCode;
import vos.DealerVo;
import vos.DeviceVo;
import vos.ReturnListVo;

/**
 * 城市等级操作类
 * 
 * @ClassName: DealerLevelController 
 * @author: wangsm
 * @date: 2016年5月31日 上午10:15:34
 */
public class RegionLevelController extends BaseController {
	/**
	 * 获取城市等级下拉框
	 *
	 * @Title: getCombobox 
	 * @return: void
	 */
	public static void getCombobox(){
		List<RegionLevel> models=RegionLevelService.getCombobox();
		List<Map<String,Object>> combos=new ArrayList<Map<String,Object>>();
		for(RegionLevel model : models){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("id", Long.toString(model.levelid));
			map.put("text", model.levelname);
			combos.add(map);
		}
		renderJSON(combos);
	}
	/**
	 * 城市等级页面
	 *
	 * @Title: regionlevel 
	 * @param menuid
	 * @return: void
	 */
	public static void regionlevel(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取城市列表
	 *
	 * @Title: getDealerLevel 
	 * @return: void
	 */
	public static void getRegionLevel(int page,int rows, String sort,String order,Boolean isflag,String levelname){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		RegionLevelSeachVo vo=new RegionLevelSeachVo(page,rows,sort,order,levelname,isflag);
		long total=RegionLevelService.countRegionLevel(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<RegionLevel> lists=RegionLevelService.listRegionLevel(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchEnable(String ids){
		if(!checkAuthority("RegionLevel_ENABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=RegionLevelService.betchEnable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量启用城市等级"+ids,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"启用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"启用失败");
		}
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDisable(String ids){
		if(!checkAuthority("RegionLevel_DISABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		String[] id=ids.split(",");
		List<String> listid=new ArrayList<String>();
		Collections.addAll(listid, id);
		boolean issuccess=RegionLevelService.betchDisable(listid,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量停用城市等级"+ids,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"停用失败");
		}
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(String ids){
		if(!checkAuthority("RegionLevel_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		String[] id=ids.split(",");
		List<String> listid=new ArrayList<String>();
		Collections.addAll(listid, id);
		for(String ss:listid){
			LevelModelPrice price=LevelModelPriceService.getByLevel(ss);
			if(price!=null){
				renderFailedJson(ReturnCode.ErrorCode,"城市等级已设置返款");
			}
			RegionLevelRelation region=RegionLevelRelationService.getByLevel(ss);
			if(region!=null){
				renderFailedJson(ReturnCode.ErrorCode,"城市等级已划分地区");
			}
		}
		boolean issuccess=RegionLevelService.betchDel(listid);
		if(issuccess){
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除城市等级"+ids,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delRegionLevel 
	 * @param id 城市等级id
	 * @return: void
	 */
	public static void delRegionLevel(String id){
		if(!checkAuthority("RegionLevel_DELETE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		LevelModelPrice price=LevelModelPriceService.getByLevel(id);
		if(price!=null){
			renderFailedJson(ReturnCode.ErrorCode,"城市等级已设置返款");
		}
		RegionLevelRelation region=RegionLevelRelationService.getByLevel(id);
		if(region!=null){
			renderFailedJson(ReturnCode.ErrorCode,"城市等级已划分地区");
		}
		boolean issuccess=RegionLevelService.delRegionLevel(id);
		if(issuccess){
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除城市等级"+id,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加城市等级
	 *
	 * @Title: addRegionLevel 
	 * @return: void
	 */
	public static void regionlevelAdd(){
		render();
	}
	/**
	 * 复制城市等级
	 *
	 * @Title: addRegionLevel 
	 * @return: void
	 */
	public static void regionlevelCopy(Long id){
		RegionLevel oldRegionLevel=RegionLevelService.getById(id);
		renderArgs.put("level", oldRegionLevel);
		render("RegionLevelController/regionlevelAdd.html");
	}
	/**
	 * 保存城市等级信息
	 *
	 * @Title: saveRegionLevel 
	 * @param regionlevel 城市等级对象
	 * @return: void
	 */
	public static void saveRegionLevel(RegionLevel level){
		if(!checkAuthority("RegionLevel_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(level==null){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级不能为空");
		}
		if(StringUtils.isEmpty(level.levelname)){
			renderFailedJson(ReturnCode.ErrorCode, "名称不能为空");
		}
		if(level.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		RegionLevel oldRegionLevel=RegionLevelService.getById(level.levelid);
		if(oldRegionLevel!=null){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级已存在");
		}
//		if(!Validate.isEnglishNum(regionlevel.regionlevelid)){
//			renderFailedJson(ReturnCode.ErrorCode, "城市等级编号格式不正确");
//		}
		level.createname=oper.operatorid;
		boolean isSuccess=RegionLevelService.saveRegionLevel(level);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"添加城市等级"+level.levelid,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑城市等级
	 *
	 * @Title: regionlevelEdit 
	 * @return: void
	 */
	public static void regionlevelEdit(Long id){
		RegionLevel oldRegionLevel=RegionLevelService.getById(id);
		renderArgs.put("level", oldRegionLevel);
		render();
	}
	/**
	 * 查看设备
	 *
	 * @Title: shopEdit 
	 * @return: void
	 */
	public static void regionlevelView(Long id){
		if(id==null){
			renderFailedJson(ReturnCode.ErrorCode, "id不能为空");
		}
		RegionLevel level=RegionLevelService.getById(id);
		renderArgs.put("level", level);
		render();
	}
	/**
	 * 保存城市等级信息
	 *
	 * @Title: saveRegionLevel 
	 * @param regionlevel 城市等级对象
	 * @return: void
	 */
	public static void updateRegionLevel(RegionLevel level){
		if(!checkAuthority("RegionLevel_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(level==null){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级不能为空");
		}
		if(StringUtils.isEmpty(level.levelname)){
			renderFailedJson(ReturnCode.ErrorCode, "名称不能为空");
		}
		if(level.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		level.modifyname=oper.operatorid;
		boolean isSuccess=RegionLevelService.updateRegionLevel(level);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"编辑城市等级"+level.levelid,"城市等级管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param regionlevelid
	 * @param regionlevelname
	 * @param shopid
	 * @param shopname
	 * @param isflag
	 * @param dealerid
	 * @param dealername
	 * @param modelid
	 * @param status
	 * @return: void
	 */
	public static void exportXls(String levelname,Boolean isflag){
		if(!checkAuthority("RegionLevel_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		RegionLevelSeachVo vo =new RegionLevelSeachVo(levelname,isflag);
		List<RegionLevel> lists=RegionLevelService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "城市等级信息"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出城市等级信息数据","城市等级管理");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.regionLevelXlsTemplate, lists), fileName);
	}
}
